from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings


def generic_email_token(email, user_id):
    # secret_key, 密钥
    # expires_in = None 过期时间
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    # 2. 组织数据
    data = {
        'email': email
        , 'user_id': user_id
    }
    # 3. 数据加密
    secret_data = s.dumps(data)

    # 4.返回数据
    return secret_data.decode()


from itsdangerous import BadTimeSignature

def check_meail_token(token):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        # 解密
        data = s.loads(token)
    except BadTimeSignature:
        return None
    else:
        return data
